package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.api.db.BlackListApi;
import com.tanhua.dubbo.api.mapper.BlackListMapper;
import com.tanhua.model.db.BlackList;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 黑名单管理服务接口实现类
 */
@DubboService
public class BlackListApiImpl implements BlackListApi {

    @Autowired
    private BlackListMapper blackListMapper;

    /**
     * 分页查询黑名单列表数据
     *
     * @param page
     * @param pagesize
     * @param userId
     * @return
     */
    @Override
    public PageResult<BlackList> blacklistPage(Long page, Long pagesize, Long userId) {
        //1.构造分页对象
        IPage<BlackList> queryPage = new Page<>(page, pagesize);
        //2.构造分页查询条件
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);//当前用户id
        //3执行分页查询
        IPage<BlackList> p = blackListMapper.selectPage(queryPage, queryWrapper);
        //4.构造PageResult对象进行返回
        return new PageResult<>(p.getTotal(), p.getSize(), p.getPages(), page, p.getRecords());
    }

    /**
     * 黑名单 - 移除
     */
    @Override
    public void removeBlackList(Long blackUserId, Long userId) {
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper();
        queryWrapper.eq("black_user_id", blackUserId);
        queryWrapper.eq("user_id", userId);
        blackListMapper.delete(queryWrapper);
    }

    //根据当前用户Id返回对应的黑名单用户Ids
    @Override
    public List<Long> findUserBlackIds(Long userId) {
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        List<Long> blackIds = new ArrayList<>();

        List<BlackList> blackLists = blackListMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(blackLists)) {
            for (BlackList blackList : blackLists) {
                blackIds.add(blackList.getBlackUserId());
            }
        }
        return blackIds;
    }
}
